package com.dlwx.parttimetalent.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.adapter.OrderPublishAdapter;
import com.dlwx.parttimetalent.base.BaseActivity;
import com.dlwx.parttimetalent.bean.MyPublishBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.ToastUtils;
import com.github.jdsjlzx.ItemDecoration.DividerDecoration;
import com.github.jdsjlzx.interfaces.OnItemClickListener;
import com.github.jdsjlzx.interfaces.OnLoadMoreListener;
import com.github.jdsjlzx.interfaces.OnRefreshListener;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyPublishActivity extends BaseActivity {

    @BindView(R.id.title_left_one_btn)
    ImageView titleLeftOneBtn;
    @BindView(R.id.tv_middle_title)
    TextView tvMiddleTitle;
    @BindView(R.id.recyclerView)
    LRecyclerView recyclerView;
    private OrderPublishAdapter mAdapter;
    private ArrayList<MyPublishBean.BodyBean> mDatas;
    private  int page =1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_publish);
        ButterKnife.bind(this);
        initView();
        getDatas();
    }

    private void getDatas() {
        loadingDialog.show();
        OkgoUtil.post(Http.Myreleasework, this)
                .params("pageno",page)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDialog.dismiss();
                        MyPublishBean parentBean = DealResult.getInstance().dealDatas(MyPublishActivity.this, response, new TypeToken<MyPublishBean>() {}.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if(parentBean.getCode()==Constant.SUCESS||parentBean.getCode()==Constant.EMPTY){
                            mAdapter.addAll(parentBean.getBody());
                            recyclerView.refreshComplete(mAdapter.getItemCount());
                        }else{
                            ToastUtils.toastForShort(MyPublishActivity.this, parentBean.getResult());
                        }

                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDialog.dismiss();
                        recyclerView.refreshComplete(mAdapter.getItemCount());
                        ToastUtils.toastForShort(MyPublishActivity.this,getString(R.string.bad_net));
                    }
                });
    }

    private void initView() {
        titleLeftOneBtn.setVisibility(View.VISIBLE);
        tvMiddleTitle.setVisibility(View.VISIBLE);
        tvMiddleTitle.setText("我的发布");
        recyclerView.setLayoutManager(new LinearLayoutManager(MyPublishActivity.this));
        mAdapter=new OrderPublishAdapter(MyPublishActivity.this);
        mDatas=new ArrayList<MyPublishBean.BodyBean>();
        mAdapter.setDataList(mDatas);
        final LRecyclerViewAdapter lRecyclerViewAdapter=new LRecyclerViewAdapter(mAdapter);
        //divider
        DividerDecoration divider = new DividerDecoration.Builder(MyPublishActivity.this)
                .setHeight(R.dimen.size_2)
//                .setPadding(R.dimen.divider_padding)
                .setColorResource(R.color.white)
                .build();
        recyclerView.setHasFixedSize(true);
        recyclerView.addItemDecoration(divider);
        recyclerView.setAdapter(lRecyclerViewAdapter);
        recyclerView.setLoadMoreEnabled(true);
        recyclerView.setPullRefreshEnabled(true);
        recyclerView.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getDatas();
            }
        });
        recyclerView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                mAdapter.clear();
                lRecyclerViewAdapter.notifyDataSetChanged();
                getDatas();
            }
        });

        lRecyclerViewAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent i=new Intent(MyPublishActivity.this, PublishDetailActivity.class);
                i.putExtra( Constant.IS_ME,true);
                i.putExtra("id",mAdapter.getDataList().get(position).getWorkid()+"");
                startActivity(i);
            }
        });
    }

    @OnClick(R.id.title_left_one_btn)
    public void onViewClicked() {
        this.finish();
    }
}
